package com.culture.bootdemo.controller;

import com.github.shyiko.mysql.binlog.BinaryLogClient;
import com.github.shyiko.mysql.binlog.BinaryLogFileReader;
import com.github.shyiko.mysql.binlog.event.*;
import com.github.shyiko.mysql.binlog.event.deserialization.EventDeserializer;

import java.io.File;
import java.io.IOException;

/**
 * @Author: wenjl
 * @Date: 2021/7/26 14:25
 * @Description: 读取mysql binlog日志文件  ==> row模式
 */
public class MysqlBinLogDemo {

    public static void main(String[] args) throws IOException {
//        readLog();
        readDB();
    }

    public static void readLog() throws IOException {
        File file = new File("C:\\ProgramData\\MySQL\\MySQL Server 5.7\\Data\\mysql-bin.000087");
        EventDeserializer eventDeserializer = new EventDeserializer();
        eventDeserializer.setCompatibilityMode(
                EventDeserializer.CompatibilityMode.DATE_AND_TIME_AS_LONG,
                EventDeserializer.CompatibilityMode.CHAR_AND_BINARY_AS_BYTE_ARRAY
        );
        try (BinaryLogFileReader reader = new BinaryLogFileReader(file, eventDeserializer)) {
            for (Event event; (event = reader.readEvent()) != null; ) {
                EventData data = event.getData();
                System.out.println(data);
                //如果日志是更新记录
                if (data instanceof TableMapEventData) {
                    System.out.println("Table:");
                    TableMapEventData tableMapEventData = (TableMapEventData) data;
                    System.out.println(tableMapEventData.getTableId() + ": [" + tableMapEventData.getDatabase() + "-" + tableMapEventData.getTable() + "]");
                }
                if (data instanceof UpdateRowsEventData) {
                    System.out.println("Update:");
                    System.out.println(data.toString());
                } else if (data instanceof WriteRowsEventData) {
                    System.out.println("Insert:");
                    System.out.println(data.toString());
                } else if (data instanceof DeleteRowsEventData) {
                    System.out.println("Delete:");
                    System.out.println(data.toString());
                }
                /*if (data != null && data.getClass().isAssignableFrom(RowsQueryEventData.class)) {
                    RowsQueryEventData dmlData = (RowsQueryEventData) data;
                    System.out.println(dmlData.getQuery());
                }*/
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void readDB() throws IOException {
        BinaryLogClient client = new BinaryLogClient("127.0.0.1", 3306, "root", "123456");
        client.setServerId(1);

        client.registerEventListener(event -> {
            EventData data = event.getData();
            if (null == data) {
                return;
            }

            if (data instanceof TableMapEventData) {
                TableMapEventData tableMapEventData = (TableMapEventData) data;
                System.out.printf("tableId: [%s]; database: [%s]; tableName: [%s]%n", tableMapEventData.getTableId(), tableMapEventData.getDatabase(), tableMapEventData.getTable());
            }
            if (data instanceof UpdateRowsEventData) {
                System.out.println("Update:" + data.toString());
            } else if (data instanceof WriteRowsEventData) {
                System.out.println("Insert:" + data.toString());
            } else if (data instanceof DeleteRowsEventData) {
                System.out.println("Delete:" + data.toString());
            }
        });

        try {
            client.connect();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
